﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using palmApp.Database;

namespace palmApp
{
    public partial class historyScreen : Form
    {
        Int32 comtec_id_machine;
        DataTable history;
        Stack<Int32> operationIndexes;

        private String SearchResponsible(Int32 id_resp)
        {
            String result = "";
            
            Manager manResponsible = new Manager();

            manResponsible.BuildDataSet(new String[] { "responsible" }, "responsible");

            DataRow foundRow = manResponsible.Ds.Tables["responsible"].Rows.Find(id_resp);

            if (foundRow != null)
            {
                result = foundRow["name"].ToString() + " " + foundRow["lastname1"].ToString();
            }

            return (result);
        }

        public historyScreen(Int32 _comtec_id_machine)
        {
            InitializeComponent();

            operationIndexes = new Stack<Int32>();

            comtec_id_machine = _comtec_id_machine;
            
            statusBar1.Text = "Máquina : " + comtec_id_machine.ToString();

            Manager manHistory = new Manager();

            manHistory.BuildDataSet(new String[] { "operation" }, "historyDataSet");

            history = manHistory.Ds.Tables["operation"];

            foreach (DataRow drCurrent in history.Rows)
            {
                if (drCurrent["comtec_id_machine"].ToString().Equals(comtec_id_machine.ToString()))
                {
                    comboBox1.Items.Add(drCurrent["operation_date"].ToString());
                    operationIndexes.Push(Convert.ToInt32(drCurrent["comtec_id_operation"].ToString()));
                }
            }

            this.ShowDialog();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 id_operation = operationIndexes.ToArray()[comboBox1.SelectedIndex];

            foreach (DataRow drCurrent in history.Rows)
            {
                if (drCurrent["comtec_id_operation"].Equals(id_operation))
                {
                    textBox1.Text = drCurrent["comtec_id_operation"].ToString();
                    textBox2.Text = drCurrent["operation_type"].ToString();
                    textBox3.Text = drCurrent["operation_date"].ToString();
                    textBox4.Text = this.SearchResponsible(Convert.ToInt32(drCurrent["comtec_id_responsible"]));
                    textBox5.Text = drCurrent["diagnosis"].ToString();
                    textBox6.Text = drCurrent["changed_parts"].ToString();
                    textBox7.Text = drCurrent["next_maintainment_date"].ToString();
                    textBox8.Text = drCurrent["maintainment_period_date"].ToString();
                }
            }
        }

        private void label9_ParentChanged(object sender, EventArgs e)
        {

        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItem2_Click_1(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void tabPage4_Click(object sender, EventArgs e)
        {

        }
    }
}